<?php
namespace system\platform\controller;


class Admin{

	//管理员首页
	function index(){   
		//查询管理员列表
		$sql="select * from ".tablename( 'sys_admin' )." where is_delete=0 ";
		if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' ){
			$keyword = trim($_REQUEST['keyword']);
			$sql .= " and name like '%$keyword%' ";
		}
		$sql .= " order by admin_id ";

		//$page=new \library\deep\Page(20);
		$page= \library\deep\Page::new_instance();

		$sql=$page->getPageSQL( $sql , 20 );

		$rs=$GLOBALS['db']->query($sql);
		
		require view('default/admin/admin.tpl.php');
	} 

	//添加管理员页面
	function admin_add(){
		require view('default/admin/admin_add.tpl.php');
	}

	//修改管理员页面
	function admin_edit(){
		if( isset($_GET['id']) && $_GET['id']!='' ){
			$id = intval( $_GET['id'] );
			$sql="select * from ".tablename('sys_admin')." where admin_id=".$id;
			$ret=$GLOBALS['db']->query($sql)->fetch();
		}else{
			message('未找到该管理员,请刷新页面',url(['platform','admin','index']));
		}
		require view('default/admin/admin_edit.tpl.php');
	}

	//保存
	function admin_save(){

		if(!isset($_POST['login_account']) && $_POST['login_account']==''){
			message('管理员账号不能为空',url(['platform','admin','index']));
		}

		$data = [
			'name'=> $_POST['name'],
			'status'=> $_POST['status'],
		];

		if( isset($_POST['id']) && $_POST['id']!=''){
			//修改
			//条件
			$where['admin_id']= intval($_POST['id']);
			//执行sql
			$ok = pdo_update('sys_admin',$data,$where);
			if($ok){
				message('修改成功',url(['platform','admin','index']));
			}else{
				message('修改失败',url(['platform','admin','index']));
			}
		}else{
			//添加
			if($_POST['login_password']!=$_POST['password2']){
				message('两次密码不一致',url(['platform','admin','index']));
			}

			$sql = "select * from " . tablename('sys_admin')." where login_account = '". trim($_POST['login_account']) . "' ";
			$ret = pdo_fetch( $sql );
			if( $ret ){
				message('登录账号已存在，请更换账号名',url(['platform','admin','admin_add']));
			}

			$data['login_account']= trim($_POST['login_account']) ;
			$data['login_password']= substr(md5($_POST["login_password"]),15,25);
			$data['create_time']= time() ;

			//执行sql
			$ok = pdo_insert( 'sys_admin' , $data );
			if($ok){
				message('添加成功',url(['platform','admin','index']));
			}else{
				message('添加失败',url(['platform','admin','index']));
			}
		}
	}
	
	//删除管理员
	function admin_delete(){
		
		if( isset($_GET['id']) && $_GET['id']!='' ){
			$id = intval( $_GET['id'] );

			$ok = pdo_update('sys_admin', ['is_delete'=>1] , ['admin_id'=>$id] );   // 软删除
			if($ok){
				message('删除成功',url(['platform','admin','index']));
			}else{
				message('删除失败',url(['platform','admin','index']));
			}
		}else{
			message('未找到该管理员,请刷新页面',url(['platform','admin','index']));
		}
	}



	//密码修改表单页面
	function password_form(){
		$id = isset($_SESSION['platform']['admin']['admin_id'])?$_SESSION['platform']['admin']['admin_id']:0 ;
		if( isset($_GET['id']) && $_GET['id']!=''){
			$id = $_GET['id'];
		}
		$sql="select * from ".tablename('sys_admin')." where admin_id=".$id;
		$ret=$GLOBALS['db']->query($sql)->fetch();

		require view('default/admin/password_form.tpl.php');
	}


	//密码保存
	function password_save(){

		if( isset($_POST['id']) && $_POST['id']!=''){
			if($_POST['login_password']!=$_POST['password2']){
				message('两次密码不一致',url(['platform','admin','password_form'],['id'=>$_POST['id']]) );
			}
			//修改
			$data['login_password']= substr(md5($_POST["login_password"]),15,25);
			//条件
			$where['admin_id']= intval($_POST['id']);
			//执行sql
			$ok = pdo_update('sys_admin',$data,$where);
			if($ok){
				message('修改成功',url(['platform','admin','index']));
			}else{
				message('修改失败',url(['platform','admin','index']));
			}
		}else{
			message('请刷新页面重试',url(['platform','admin','index']));
		}
	}


}
